<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */
    
    /*********************************************************
    *            --== InArch ==--
    *
    * Ajax Add Datablock
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 06.10.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/

    /***
    * class ajaxAddDatablock
    ****
    * @package ajax
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 06.10.2005
    * @copyright essentialmind.com 2005
    ***/
    class ajaxAddDatablock extends ajaxAction{

        public function process(){
            $nodeid = (int)$_REQUEST['node'];
            $pageid = (int)$_REQUEST['page'];
            $block  = (int)$_REQUEST['block'];
            
            //presume status is true
            $this->r->status = true;
            
            //need to check valid tags as well
            $tag          = $this->db->getOne("SELECT tag FROM datablocks WHERE id = $block");
            $this->r->tag = $tag;
            
            //add mapping
            if($pageid > 0){
                //first check if there is already a block with that tag at this page
                if($tit = $this->db->getOne("SELECT title FROM datablock_page_mapping AS dpm LEFT JOIN datablocks AS d ON dpm.blockid = d.id WHERE dpm.pageid = $pageid AND d.tag = '$tag'")){
                    $this->r->status = false;
                    $this->r->title  = $tit;
                }else{
                    $this->db->Execute("INSERT INTO datablock_page_mapping(blockid,pageid) VALUES($block,$pageid)");
                }
                
            }else{
                //check for a tag
                if($tit = $this->db->getOne("SELECT title FROM datablock_node_mapping AS dpm LEFT JOIN datablocks AS d ON dpm.blockid = d.id WHERE dpm.nodeid = $nodeid AND d.tag = '$tag'")){
                    $this->r->status = false;
                    $this->r->title  = $tit;
                }else{
                    $this->db->Execute("INSERT INTO datablock_node_mapping(blockid,nodeid) VALUES($block,$nodeid)");
                }
            } 
        }
    }
?>